<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\CategoryAttribute;
use App\Models\ProductAttribute;
use App\Commands\OperationLogCommand;
use App\Interfaces\CategoryService;

class CategoryAttributeController extends Controller
{

    public function index()
    {
        //dd("dgag");
    }
    /**
     * 向分类下添加品牌
     *
     * @param  int  $cat_id
     * @return \Illuminate\Http\Response
     */
    public function create($cat_id,CategoryAttribute $categoryAttribute)
    {
        return view('admin.category.addAttribute',compact(['cat_id', 'categoryAttribute']));
    }

    public function store(Request $request,CategoryAttribute $categoryAttribute)
    {
        $param = $request->all();

//        $categoryAttributeArr = $categoryAttribute->lists('attribute_name')->toArray();
//        $is_categoryAttribute = in_array($request->get('attribute_name'),$categoryAttributeArr);
//        if($is_categoryAttribute){
//            return redirect::back()->withInput()->with('message','该属性已存在');
//        }

        $categoryAttribute = $categoryAttribute->create($param);

        $this->dispatch(
            new OperationLogCommand('添加分类属性','CategoryAttribute','id:'.$categoryAttribute->cat_id)
        );
        return Redirect('admin/category/category_attribute/'.$categoryAttribute->cat_id);
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        //
    }

    public function update(Request $request,$id)
    {
        //dd('123');
        $result = ['status'=>1, 'message'=>'', 'data'=>''];
        //dd($request->all());
        $categoryAttribute = CategoryAttribute::find($id);
        //dd($categoryAttribute);
        $categoryAttribute = $categoryAttribute->update($request->all());
        $this->dispatch(
            new OperationLogCommand('修改属性','CategoryAttribute','id:'.$id)
        );
        if($categoryAttribute){
            $result['data']=$request->get('name');
        }else{
            $result['status']=0;
            $result['message']='修改失败';
        }
        return $result;
    }
    /**
     * 排序
     * @route admin.activity.sort
     * @return Response
     */
    public function sort(Request $request)
    {
        $url = $request->get('url');
        $categoryAttribute = $request->get('sort_order');
        if (is_array($categoryAttribute)&&count($categoryAttribute)>0)
        {
            foreach($categoryAttribute as $k=>$v)
            {
                $sort_order = CategoryAttribute::where('sort_order',$v)->get();

                CategoryAttribute::find($k)->update(['sort_order' => $v]);
            }
        }
        return redirect($url);
    }
    /**
     * 删除分类下的属性
     *
     * @param  int  $cat_id：分类id
     * @param  int  $id：属性id
     * @return \Illuminate\Http\Response
     */
    public function delCategoryAttribute(Request $request){

        $result = ['status'=>1, 'message'=>'', 'data'=>''];
        $id = $request->get('id');
        $cat_id = $request->get('cat_id');
//        $categoryAttribute = CategoryAttribute::where('cat_id',$cat_id)->where( 'id', $id)->delete();
        /* 删除的限制条件*/
        $productAttribute = ProductAttribute::where('category_attribute_id',$id)->get()->toArray();
        //dd($productAttribute);
        if($productAttribute){
            $result['status']=0;
            $result['message']='该属性已被使用，不能删除！';
        }else{
            $categoryAttribute = CategoryAttribute::where('cat_id',$cat_id)->where( 'id', $id)->delete();

        }
        $this->dispatch(
            new OperationLogCommand('删除属性','CategoryAttribute','id:'.$id)
        );
        return $result;
    }//end func categoryBrand()

    public function categoryAttribute($cat_id){

        $categoryAttribute = CategoryAttribute::where('cat_id',$cat_id)->orderBy('sort_order','desc')->orderBy('id','desc')->paginate(10);
        return view('admin.category.attribute',compact(['cat_id', 'categoryAttribute']));
    }

    public function getCategoryAttribute(Request $request, CategoryService $categoryService){
        $cat_id = $request->get('cat_id');
        $categoryAttribute = $categoryService->getCategoryAttribute($cat_id);

        return response()->json($categoryAttribute);

    }



}
